package cn.itsource.util;

import lombok.extern.slf4j.Slf4j;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import java.io.*;
import java.util.Map;

@Slf4j
public class JxlsUtils {
    static{

    }

    /**
     * 根据模板生成文件，直接下载
     * @param os 流数据
     * @param templateFileName 模板文件名称
     * @param model 填充数据
     * @throws IOException
     */
    public static void exportExcelWithOS(OutputStream os , String templateFileName, Map<String, Object> model) throws IOException {
        // 获取模板文件
        InputStream is = new FileInputStream("D:\\Java\\Javawork\\JavaWeb\\pethome\\src\\main\\resources\\test.xlsx");
        try {
            // 输出
            Context context = new Context(model);
            JxlsHelper.getInstance().processTemplate(is, os, context);
//            JxlsHelper.getInstance().setUseFastFormulaProcessor(false).setEvaluateFormulas(true).processTemplate(is, os, context);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            log.error(ex.getMessage());

        } finally {
            is.close();
        }
    }

}
